/*
 * @Author: gonglf
 * @Date: 2022-08-22 15:27:00
 * @LastEditors: gonglf
 * @LastEditTime: 2022-08-31 15:04:31
 * @Description: 发布消息
 *
 */
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"gitee.com/network-learning/rpc/7gRPC_oneStream/pb"
	"google.golang.org/grpc"
)

func main() {
	conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	client := pb.NewPubsubServiceClient(conn)

	for i := 0; i < 10; i++ { //测试stream是不是重复的断开和连接
		stream, err := client.Publish(context.Background()) //这里会得到一堆的连接
		if err != nil {
			log.Fatal(err)
		}
		msg := fmt.Sprintf("时间:%s-序号:%d", time.Now().Format("2006-01-02 15:04:05"), i)
		stream.Send(&pb.String{Value: msg})
		stream.Send(&pb.String{Value: msg})

		log.Println(err)
		if err != nil {
			log.Fatal(err)
		}
		time.Sleep(1 * time.Second)
	}

	time.Sleep(1 * time.Second) //这边要有一个sleep，否则还没发进程就退出了
}
